<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/9/30
 * Time: 10:23
 */

class Expressrecord extends CS_Controller
{
    public function _init()
    {
        $this->load->library('pagination');
        $this->load->helper('common');
        $this->load->model('express_record_model', 'express_record');
        $this->load->model('express_model', 'express');
    }

    public function index($pg = 1)
    {
        $page_num = 20;
        $num = ($pg-1)*$page_num;
        $config['first_url'] = base_url('expressrecord/index').$this->pageGetParam($this->input->get());
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url'] = base_url('expressrecord/index');
        $config['total_rows'] = $this->express_record->total($this->input->get());
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['pg_list'] = $this->express_record->page_list($page_num, $num, $this->input->get());
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['express'] = $this->express->findAll();

        $this->load->view('expressrecord/index', $data);
    }

    public function detail($id)
    {
        $result = $this->express_record->findById($id);
        if ($result->num_rows() <= 0) {
            $this->error('expressrecord/index', '', '快递编码错误');
        }
        $data['row'] =  $result->row();
        $this->load->view('expressrecord/detail', $data);
    }

    public function send()
    {
        $data['express'] = $this->express->findAll();
        $this->load->view('expressrecord/send', $data);
    }

    public function sendPost()
    {
        $company = $this->input->post('company');
        $number  = $this->input->post('number');
        if (!$company || !$number) {
            $this->error('expressrecord/send', '', '请选择快递类型，填写快递单号');
        } else {
            if (validExpressNu($number)) {
                $this->error('expressrecord/send', '', '快递单号只能是5位以上的数字、字母组成');
            }
            $this->sendExpress($company, $number, $this->input->post('from'), $this->input->post('to'));
        }
        $this->success('expressrecord/send', '', '推送成功！');
    }

    public function sendExpress($company, $number)
    {
        $this->load->library('express', null, 'expressLibrary');
        $result = $this->expressLibrary->send($company, $number);
        if ($result) {
            $express = $this->express->findByName($company)->row();
            $expressRecord = $this->express_record->findByNu($number);
            if ( $expressRecord->num_rows() <= 0){
                $status = $this->express_record->insert($express->name, $company, $number);
                if (!$status) {
                    $this->error('expressrecord/send', '', '推送失败(数据库插入失败)');
                }
            }
        } else {
            $this->error('expressrecord/send', '', '推送失败('.$this->expressLibrary->getMessage().')');
        }
    }

    //慎用
    public function sendAll()
    {
        $order = $this->express_record->findOrder();
        $this->load->library('express', null, 'expressLibrary');
        foreach($order->result() as $value){
            if (!$value->id) {
                $result = $this->expressLibrary->send($value->logistics_company, $value->logistics, '', '');
                if ($result) {
                    $express = $this->express->findByName($value->logistics_company)->row();
                    $result = $this->express_record->insert($express->name, $value->logistics_company, $value->logistics);
                    if (!$result) {
                        $this->error('expressrecord/send', '', '推送失败(数据库插入失败)');
                    }
                }
            }
        }
    }
}